package com.example.demo.repository.doris;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.doris.Book;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BookRepository extends BaseMapper<Book> {
      /**
     * 根据ISBN查询图书
     * @param isbn ISBN编号
     * @return 图书信息
     */
    @Select("SELECT isbn, title, author, publisher, publicationDate, placeOfPublication, size, " +
            "classificationNo, subject, series, requisitionNo, pageNo, price, requestNo, " +
            "barcodeNo, currentLocation, currentLocation2, collectionStatus, circulationCategory " +
            "FROM books WHERE isbn = #{isbn} LIMIT 1")
    Book getBookByIsbn(@Param("isbn") String isbn);
    
    /**
     * 根据书名模糊查询
     * @param title 书名关键字
     * @return 书籍列表
     */
    @Select("SELECT isbn, title, author, publisher, publicationDate, placeOfPublication, size, " +
            "classificationNo, subject, series, requisitionNo, pageNo, price, requestNo, " +
            "barcodeNo, currentLocation, currentLocation2, collectionStatus, circulationCategory " +
            "FROM books WHERE title LIKE CONCAT('%', #{title}, '%')")
    List<Book> findByTitleContaining(@Param("title") String title);
    
    /**
     * 根据作者模糊查询
     * @param author 作者关键字
     * @return 书籍列表
     */
    @Select("SELECT isbn, title, author, publisher, publicationDate, placeOfPublication, size, " +
            "classificationNo, subject, series, requisitionNo, pageNo, price, requestNo, " +
            "barcodeNo, currentLocation, currentLocation2, collectionStatus, circulationCategory " +
            "FROM books WHERE author LIKE CONCAT('%', #{author}, '%')")
    List<Book> findByAuthorContaining(@Param("author") String author);
      /**
     * 复合条件分页查询
     * @param page 分页参数
     * @param title 书名关键字
     * @param author 作者关键字
     * @param publisher 出版社关键字
     * @return 分页结果
     */
    @Select("<script>" +
            "SELECT isbn, title, author, publisher, publicationDate, placeOfPublication, size, " +
            "classificationNo, subject, series, requisitionNo, pageNo, price, requestNo, " +
            "barcodeNo, currentLocation, currentLocation2, collectionStatus, circulationCategory " +
            "FROM books" +
            "<where>" +
            "    <if test=\"title != null and title != ''\">" +
            "        AND title LIKE CONCAT('%', #{title}, '%')" +
            "    </if>" +
            "    <if test=\"author != null and author != ''\">" +
            "        AND author LIKE CONCAT('%', #{author}, '%')" +
            "    </if>" +
            "    <if test=\"publisher != null and publisher != ''\">" +
            "        AND publisher LIKE CONCAT('%', #{publisher}, '%')" +
            "    </if>" +
            "</where>" +
            "</script>")
    Page<Book> findBooks(Page<Book> page, 
                        @Param("title") String title, 
                        @Param("author") String author, 
                        @Param("publisher") String publisher);
      /**
     * 使用Doris全文索引进行模糊搜索，支持分页
     * @param page 分页参数
     * @param keyword 搜索关键词
     * @return 分页结果
     */
    @Select("SELECT isbn, title, author, publisher, publicationDate, placeOfPublication, size, " +
            "classificationNo, subject, series, requisitionNo, pageNo, price, requestNo, " +
            "barcodeNo, currentLocation, currentLocation2, collectionStatus, circulationCategory " +
            "FROM books WHERE title MATCH_ALL #{keyword}")
    Page<Book> searchByFullText(Page<Book> page, @Param("keyword") String keyword);    /**
     * 执行自定义SQL查询总记录数
     * @param sql 自定义SQL
     * @return 总记录数
     */
    @Select("${sql}")
    Long selectCount(@Param("sql") String sql);
}